import { useConfigStore } from '@/stores/modules/configStore';
const configStore = useConfigStore();
const { routerConfig } = configStore;
// 白名单路由集合
const status = {
  // 直接访问页面，白名单无效
  whitelist: routerConfig.whitelist
};

const jump = (url) => {
  const app = getApp();
  console.log(url);
  const noParamsUrl = url.split('?')[0];
  const isGo = useRouteInterception(noParamsUrl);
  console.log(isGo);
  console.log(noParamsUrl);
  if (isGo) {
    if (url === '/pages/login/login' && app.globalData.isLogin) reLaunch('/pages/map/map');
    else navigate(url);
  } else reLaunch('/pages/login/login?url=' + url);
};

// 纯跳转，无限制
const redirect = (url) => {
  console.log(url, 5555);
  uni.redirectTo({
    url: url
  });
};

const navigate = (url) => {
  uni.navigateTo({
    url: url
  });
};

const reLaunch = (url) => {
  uni.reLaunch({
    url: url
  });
};

// 路由拦截
const useRouteInterception = (url) => {
  const app = getApp();
  // 判断是否登录
  if (app.globalData.isLogin) return true;
  // 无登录下，白名单路由是否匹配
  console.log(url);
  const isGo = status.whitelist.includes(url);
  if (isGo) return true;
  return false;
};

export default { jump, redirect, reLaunch };
